<template>
    <div>
    <basic-container>
      <avue-crud
        :option="tableOption"
        :data="tableData"
        :table-loading="tableLoading"
        :page="page"
        ref="crud"        
        @refresh-change="handleRefreshChange"
        @current-change="handleCurrentChange"
        @size-change="handleSizeChange"
        @selection-change="handleSelectionChange"
        @search-change="handleSearchChange"
      >

    <template slot-scope="scope" slot="menu">
          <!-- <el-button
            type="text"
            @click.stop="handleResolve(scope.row)"
            icon="el-icon-edit"
            size="mini"
            v-if="scope.row.realAuthStatus == 1"
            v-perm="'/admin/user/check_real_auth'"
          >审核通过</el-button>
          <el-button
            type="text"
            @click.stop="handleReject(scope.row)"
            icon="el-icon-edit"
            size="mini"
            v-if="scope.row.realAuthStatus == 1"
            v-perm="'/admin/user/check_real_auth'"
          >审核拒绝</el-button> -->
          <!-- <el-button
            type="text"
            @click.stop="handleResetPwd(scope.row)"
            icon="el-icon-edit"
            size="mini"
            v-perm="'/admin/user/reset-pwd'"
          >重置密码</el-button> -->
          <!-- <el-button
            type="text"
            v-if="scope.row.status == 1"
            @click.stop="handleStopUser(scope.row)"
            icon="el-icon-edit"
            size="mini"
            v-perm="'/admin/user/check-user-status'"
          >停用</el-button>
          <el-button
            type="text"
            v-if="scope.row.status == 0"
            @click.stop="handleStartUser(scope.row)"
            icon="el-icon-edit"
            v-perm="'/admin/user/check-user-status'"
            size="mini"
          >启用</el-button> -->
        </template>
          <template slot="menuLeft">
            <el-button
          type="primary"
          @click="handleExport"
          :loading="downloadLoading"
          icon="el-icon-download"
          v-perm="'/admin/user/export'"
          size="small"
        >导出</el-button>
          <!-- <el-button
            type="primary"
            @click="handleBathResolve"
            icon="el-icon-edit"
            v-perm="'/user/batch_check_real_auth'"
            size="small"
          >批量审核通过</el-button>
          <el-button
            type="danger"
            @click="handleBathReject"
            icon="el-icon-edit"
            v-perm="'/user/batch_check_real_auth'"
            size="small"
          >批量审核拒绝</el-button> -->
        </template>
      </avue-crud>
      </basic-container>
    </div>
  </template>
  <script>
  import { validatenull } from "@/util/validate";
  import { filterForm } from '@/util/util';
  // import { DIC } from "@/const/dic";
  import {  getLabelFromDicV2 } from "@/const/dic";
  import { nowDate } from "../../util/date";
  import * as apiObj from "../../api/userInfo";
  import { userInfoTableOption} from "@/const/userInfo/userInfoTableOption.js";
  
  export default {
    data() {
      return {
        bachDeleting: false,
        tableOption: {}, //表格设置属性
        tableData: [], //表格的数据
        downloadLoading:false,
        tableLoading: false,
        tableQueryParam: {},
        page: {
          total: 0, //总页数
          currentPage: 1, //当前页数
          pageSize: 10 //每页显示多少条
        }
      };
    },
    created() {
      //初始化数据格式
      this.tableOption = userInfoTableOption;
      this.handleList();
    },
    methods: {
      handleBathResolve() {
      if (
        !this.currentSelectedRowIds ||
        this.currentSelectedRowIds.length == 0
      ) {
        this.showError("请选择至少一条记录");
        return;
      }
      const ids = this.currentSelectedRowIds.filter(el=>el.realAuthStatus == 1).map(el=>el.id);
      if(!ids.length) {
        this.showError("请选择至少一条待审核记录");
        return;
      }
      this.$prompt('确认审核通过所选用户吗', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          inputPlaceholder:'审核备注'
        }).then(({ value }) => {
            value = value || '';
            apiObj.batchCheckRealAuth({
              ids,
              checkStatus:10,
              remark:value.trim()
          }).then(() => {
            this.success();
            this.handleList();
          });
          this.success();
        }).catch(() => {
          
        });
    },
    handleBathReject() {
      if (
        !this.currentSelectedRowIds ||
        this.currentSelectedRowIds.length == 0
      ) {
        this.showError("请选择至少一条记录");
        return;
      }
      const ids = this.currentSelectedRowIds.filter(el=>el.realAuthStatus == 1).map(el=>el.id);
      if(!ids.length) {
        this.showError("请选择至少一条待审核记录");
        return;
      }
      this.$prompt('确认审核拒绝所选用户吗', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          inputPlaceholder:'审核备注'
        }).then(({ value }) => {
            value = value || '';
            apiObj.batchCheckRealAuth({
              ids,
              checkStatus:5,
              remark:value.trim()
          }).then(() => {
            this.success();
            this.handleList();
          });
          this.success();
        }).catch(() => {
          
        });
    },
      handleStopUser(row) {
      this.showConfirm("确定停用该账户吗", () => {
        apiObj.checkUserStatus({
          id:row.id,
          status:0
        }).then(() => {
          row.status = 0;
          this.showSuccess();
        });
      });
    },
    handleStartUser(row) {
      this.showConfirm("确定启用该账户吗", () => {
        apiObj.checkUserStatus({
          id:row.id,
          status:1
        }).then(() => {
          row.status = 1;
          this.showSuccess();
        });
      });
    },
    handleResolve(row) {
      this.$prompt('确认审核通过吗', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          inputPlaceholder:'审核备注'
        }).then(({ value }) => {
            value = value || '';
            apiObj.checkRealAuth({
              id:row.id,
              checkStatus:10,
              remark:value.trim()
          }).then(() => {
            this.success();
            this.handleList();
          });
          this.success();
        }).catch(() => {
          
        });
    },
    handleResetPwd(row) {
      this.showConfirm("确认重置密码吗?", () => {
        apiObj.resetPwd(row.id).then(data => {
          this.alert("重置密码成功，请使用新密码:" + data);
        });
      });
    },
    handleReject(row) {
      this.$prompt('确认审核拒绝吗', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          inputPlaceholder:'审核备注'
        }).then(({ value }) => {
            value = value || '';
            apiObj.checkRealAuth({
              id:row.id,
              checkStatus:5,
              remark:value.trim()
          }).then(() => {
            this.success();
            this.handleList();
          });
          this.success();
        }).catch(() => {
          
        });
    },
      handleCurrentChange(page) {
        this.page.currentPage = page;
        this.handleList();
      },
      handleSizeChange(size) {
        this.page.pageSize = size;
        this.handleList();
      },
    
      handleSelectionChange(args) {
        this.currentSelectedRowIds = args;
      },
      hasChanged(params1,params2){
        return Object.keys(params1).some(key=>params2[key] != params1[key]);
    },
    handleSearchChange(params) {
      if(this.hasChanged(params,this.tableQueryParam)){
        this.page.currentPage = 1;
      }
        this.tableQueryParam = params;
        this.handleList();
      },
            
      
      handleRefreshChange() {
        this.handleList();
      },
      /**
       * @title 获取数据
       * @detail 赋值为tableData表格即可
       *
       **/
      handleList() {
        this.tableLoading = true;
        apiObj.list({
          pageNo: this.page.currentPage,
          pageSize: this.page.pageSize,
          ...this.tableQueryParam
        })
          .then(data => {
            this.tableData = data.dataList || [];
            this.page.total = data.total;
            this.tableLoading = false;
          })
          .catch(() => {
            this.tableLoading = false;
          });
      },     
          
      handleExport() {        
        this.downloadLoading = true;
        apiObj.list({
          pageNo: 1,
          pageSize: 3000,
          ...filterForm(this.$refs.crud.searchForm)
        }).then(res => {
          import("@/vendor/Export2Excel").then(excel => {
            const tHeader = ["状态","手机号码","昵称","注册时间","创建时间","修改人","修改时间"];
            const filterVal = ["status","mobile","nickName","registerTime","createTime","modifier","modifyTime"];
            const data = this.formatJson(filterVal, res.dataList);
            excel.export_json_to_excel({
              header: tHeader,
              data,
              filename: "用户管理" + nowDate()
            });
            this.downloadLoading = false;
          });
        });
      },
      formatJson(filterVal, jsonData) {      
        return jsonData.map(v =>
          filterVal.map(j => {
            return getLabelFromDicV2(this.$refs.crud.DIC,j,v[j]);
          })
        );
      }
    }
  };
  </script>
